//
//  MyOpinionListController.m
//  Stock
//
//  Created by sun black on 11-10-16.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "OpinionMonitorController.h"
#import "StockListController.h"
#import "MyStock.h"

@implementation OpinionMonitorController
@synthesize service;


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.tableView.rowHeight=44.0;
    self.service = [[[StockAppService alloc] initWithDelegate:self] autorelease];
    [super viewDidLoad];
}


-(void)queryStocks{
    [super queryStocks];
    [service queryNewOpinionWithIndex:currentPage andType:nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
}
-(void)dealloc{
    [self.service cancleAllRequest];
    self.service = nil;
    self.key = nil;
    [super dealloc];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [quotationStocks count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"OpinionListCell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        UIViewController *controller = [[UIViewController alloc] initWithNibName:@"OpinionMonitorCell" bundle:nil];
        [cell.contentView addSubview:controller.view];
        [controller release];
    }    
    
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg2.png";
    if (index%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    NSString *name = [dict objectForKey:@"name"];
    NSString *firedDate = [[dict objectForKey:@"firedDate"] substringFromIndex:5];
    NSString *pubDate = [[dict objectForKey:@"pubDate"] substringFromIndex:5];
    NSInteger days = [[dict objectForKey:@"days"] intValue];
    float growth = [[dict objectForKey:@"growth"] floatValue];
    NSString *growthStr = [NSString stringWithFormat:@"%.1f%%",growth*100];
    
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:51];
    UILabel *growthLabel = (UILabel *)[cell viewWithTag:52];
    UILabel *pubLabel = (UILabel *)[cell viewWithTag:53];
    UILabel *daysLabel = (UILabel *)[cell viewWithTag:54];
    
    nameLabel.text = name;
    growthLabel.text = growthStr;
    pubLabel.text = [NSString stringWithFormat:@"%@早间推荐",pubDate];
    if (days==1) {
        daysLabel.text = [NSString stringWithFormat:@"%@单日涨幅",firedDate];
    }else{
        daysLabel.text = [NSString stringWithFormat:@"%@起%d日内累计涨幅",firedDate,days];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = [quotationStocks objectAtIndex:indexPath.row];
    
    MyStock *myStock = [[MyStock alloc] init];
    myStock.name = [dict objectForKey:@"name"];
    myStock.uuid = [dict objectForKey:@"id"];
    
    [StockContext shareInstance].remoteMyStocks = [NSMutableArray arrayWithObject:myStock];
    
    StockListController *stockListController = [[StockListController alloc] initWithNibName:@"StockListController" bundle:nil];
    stockListController.myStock = myStock;
    StockAppDelegate *delegate = (StockAppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.navigationController pushViewController:stockListController animated:YES];
    [stockListController release];
    
    [myStock release];
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}


@end
